<template>
	<view class="baseForm">
		<!-- 搜索&扫码 -->
		<view class="upload-scan">
			<u-search v-model="searchText" shape="round" bgColor="#fff" :show-action="true" actionText="搜索"
				placeholder="请输入模具名称" :animation="true" :clearabled="true" class="search" @search="searchList"
				@clear="clearList" @custom="searchList"></u-search>
			<!-- <u-icon name="scan" size="27px" :bold="false" @click="openScan"></u-icon> -->
		</view>

		<!-- 当前作业 -->
		<view class="activeHeight">
			<!-- 列表 -->
			<u-list ref="tableScroll" v-if="tableList?.length > 0" @scrolltolower="scrolltolower" class="list"
				:height="listHeight" lowerThreshold="50px" :pagingEnabled="true">
				<u-list-item v-for="(item, index) in tableList" :key="item.id" @click="jump(item)">
					<view class="card">
						<view class="flex record-title">
							<text class="line1">模具编号</text>
							<text>
								{{ item.assetCode }}
							</text>
						</view>
						<view class="flex flex-col justify-between flex-1 h-full record-content-body">
							<view class="record-content">
								<view class="record-item">
									<text>模具名称</text>
									<text>{{ item.assetName }}</text>
								</view>
								<view class="record-item">
									<text>产品编号</text>
									<text>{{ item.productCode }}</text>
								</view>
								<view class="record-item">
									<text>产品名称</text>
									<text>{{ item.productName }}</text>
								</view>
								<view class="record-item">
									<text>项目名称</text>
									<text>{{ item.projectName }}</text>
								</view>
							</view>
						</view>
					</view>
				</u-list-item>
			</u-list>
			<view class="flex justify-center" v-show="tableLoading">
				<u-loading-icon color="#000a7b"></u-loading-icon>
			</view>
			<!-- 空数据 -->
			<view v-show="tableList.length === 0 && !tableLoading">
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { ref, toRefs } from 'vue';
	import { onShow, onPullDownRefresh, onLoad } from '@dcloudio/uni-app';
	import { http } from '@/utils/http';
	import { PageResult, TabInfo } from '@/types/type';
	import { getSecondTime } from '@/utils/common';
	import { useDictionaryStore } from '@/stores/useDictionaryStore';
	import { useEamStore } from '@/stores/useEamStore';

	// const activeTab = ref(0);
	// const tabList = [{ name: '所有' }, { name: '待处理' }, { name: '处理中' }, { name: '已完成' }];
	// function changeTab(val: TabInfo) {
	// 	activeTab.value = val.index;
	// 	searchList();
	// }
	const props = defineProps({
		equipCode: {
			type: String,
			default: () => ''
		},
		processCode: {
			type: String,
			default: () => ''
		},
		moldCode: {
			type: String,
			default: () => ''
		},
		type: {
			type: String,
			default: () => ''
		}
	});



	// 待完成数据
	const tableList = ref([]);
	const tableLoading = ref(false);
	const current = ref(0);
	const totalPage = ref(0);
	const total = ref(0);

	//列表高度
	const listHeight = ref('');

	onLoad(() => {
		// #ifdef H5
		listHeight.value = 'calc(100vh - 88px)';
		// #endif

		// #ifdef APP-PLUS
		listHeight.value = 'calc(100vh - 48px)';
		// #endif
		
	searchText.value=	props.moldCode||''
		loadmore();
		
	});

	onPullDownRefresh(() => {
		searchList();
	});

	async function loadmore(type1 ?: string) {
		try {
			current.value += 1;
			tableLoading.value = true;
			const result: PageResult<any> = await http({
				method: 'POST',
				url: '/eam/mouldAssetInfo/queryByPage',
				data: {
					current: current.value,
					size: 20,
					data: props.type=='1'? {
						assetName: searchText.value,
						storageStatus:0
					}:{
						assetName: searchText.value,
						enabled:1,
						assetStatusList:[1,3],
						storageStatus:1
					}
				}
			}).finally(() => {
				tableLoading.value = false;
			});
			const list = result.data;
			for (let i = 0; i < list.length; i++) {
				tableList.value.push(list[i]);
			}
			totalPage.value = result.page;
			total.value = result.total;
			type1 === 'refresh' && uni.stopPullDownRefresh();
		} catch (e) {
			console.log(e);
		}

		// await http({
		// 	method: 'POST',
		// 	url: '/eam/modbomgroup/queryProduct',
		// 	data: {
		// 		processCode: props.processCode,
		// 		equipCode: props.equipCode ? props.equipCode : '',
		// 		moldCode: props.moldCode ? decodeURIComponent(props.moldCode) : '',
		// 		productCode: searchText.value
		// 	}
		// }).then((res) => {
		// 	tableList.value = res.data
		// })
		// 	.finally(() => {
		// 		tableLoading.value = false;
		// 	});

	}
	const scrolltolower = () => {
		totalPage.value > current.value && loadmore();
	};

	// 点击扫码
	const searchText = ref('');
	function openScan() {
		/* #ifdef H5 */
		uni.showToast({
			icon: 'none',
			title: '请到安卓产品进行扫码'
		});
		/* #endif */
		// 只允许通过相机扫码
		uni.scanCode({
			scanType: ['barCode', 'qrCode'],
			onlyFromCamera: true,
			success: function (res) {
				searchText.value = JSON.stringify(res.result);
				searchList();
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
			}
		});
	}

	// 列表搜索
	function searchList() {
		current.value = 0;
		tableList.value = [];
		loadmore('refresh');
	}

	function clearList() {
		console.log('点击');
		searchText.value = '';
		searchList();
	}

	// 网页跳转
	async function jump(item) {
		await useEamStore().setMoldDataData(item);
		uni.navigateBack();
		// uni.navigateTo({
		// 	url: `/pages/eam/equ/repairOrderCreate/index?assetId=${item.id}`,
		// 	fail: () => {
		// 		uni.showToast({
		// 			title: '敬请期待',
		// 			icon: 'error'
		// 		});
		// 	}
		// });
	}
</script>

<style lang="scss" scoped>
	uni-page-body {
		height: 100%;
		background-color: #fbfbff;
	}

	.search {
		:deep(.u-search__content) {
			box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
		}
	}

	.baseForm {
		background-color: #fbfbff;

		.tab-bar {
			padding: 0 20px;
		}

		.upload-scan {
			padding: $uni-page-padding;
			padding-top: 5px;
			padding-bottom: 5px;
			display: flex;
			align-items: center;

			// box-shadow: 0 1px 10px 0px rgb(19 10 236 / 10%);
			.u-icon {
				margin-left: 8px;
			}
		}

		.u-line {
			padding: 10px 0;
		}
	}

	.list {
		.card {
			margin: $uni-page-padding;
			margin-top: 5px;
			margin-bottom: 4px;
			border-radius: 7px;
			padding: 12px 20px 0px 20px;
			transition: all 300ms ease;
			border: 1px solid #efefef;
			background-color: #fff;

			.card-flex {
				display: flex;
				align-items: flex-start;
			}

			.right {
				flex: 1;
			}

			.title {
				font-size: 14px;
				line-height: 40px;
				height: 40px;
				margin-top: 8px;
				font-weight: bold;
				color: #101010;
				border-top: 1px solid #e5ebff;
			}

			.uni-btn {
				float: right;
				font-size: 12px;
				height: 24px;
				line-height: 24px;
				padding: 0 15px;
				margin-top: 7px;
				font-weight: 400;
			}

			.des {
				font-size: 12px;
				line-height: 20px;
				margin-bottom: 3px;
			}

			.left {
				flex-basis: calc(50% + 10px);
			}
		}
	}

	.record-title {
		display: flex;
		justify-content: space-between;
		margin: 2px 0 2px 0;
		color: $primary-color;

		uni-text:nth-child(1) {
			font-weight: bold;
			font-size: 15px;
		}

		uni-text:nth-child(2) {
			font-weight: bold;
			font-size: 15px;
			min-width: 60px;
			text-align: right;
		}
	}

	.record-item {
		margin: 7px 0 !important;
		color: #8f8e94 !important;
	}
</style>